package com.zds.support.api.retrofit.function;



import com.zds.support.api.retrofit.exception.ApiException;
import com.zds.support.api.retrofit.response.ApiPageResponse;

import java.util.ArrayList;

import io.reactivex.functions.Function;

public class ApiPageResponseFunction<T> implements Function<ApiPageResponse<T>, ArrayList<T>> {
    @Override
    public ArrayList<T> apply(ApiPageResponse<T> apiResponse) throws Exception {
        if (apiResponse.isSuccess()) {
            ApiPageResponse.Data<T> data = apiResponse.getData();
            if (data != null) {
                return data.getList();
            } else {
                return new ArrayList<T>();
            }

        } else {
            throw new ApiException(apiResponse.getCode(), apiResponse.getMsg());
        }
    }
}
